<template>
  <!-- 退款 -->
  <el-dialog title="退款" :visible="value" width="700px" custom-class="dialog-box" @close="onClose" @open="onOpen" :close-on-click-modal="false" :append-to-body="true">
    <el-form ref="form" :model="form" :rules="rules" label-width="160px" @submit.native.prevent="onSubmit">
      <el-row>
        <el-col :span="20">
          <el-form-item label="姓名：">
            <label>{{curItem.name}}</label>
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="账户余额：">
            <label v-if="curItem!=null">￥{{curItem.patientMoney}}</label>
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="赠送余额：">
            <label v-if="curItem!=null&&curItem.grandPresenter!=''">￥{{curItem.grandPresenter}}</label>
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="退款金额：" prop="rechargeMoney">
            <el-input v-model="form.rechargeMoney"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="赠送金额：" prop="giveMoney">
            <el-input v-model="form.giveMoney">
              <template slot="prepend">扣除</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="剩余赠送金额：">
            <label>￥0</label>
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="备注：">
            <el-input v-model="form.desc"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="退款方式" prop="source">
            <el-select v-model="form.source">
              <el-option label="现金" value="7" :key="7">现金</el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-button v-show="false" :loading="btnLoading" native-type="submit">提交</el-button>
    </el-form>
    <span slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
      <el-button :loading="btnLoading" @click="onClose">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import Valid from "@/_js/validate.js";
export default {
  props: ["value", "curItem"],
  data() {
    return {
      btnLoading: false,
      form: {
        patientId: "",
        rechargeMoney: "",
        desc: "",
        giveMoney: "",
        source: "7"
      },
      code: "",
      vipList: [],
      rules: {
        rechargeMoney: [
          { required: true, message: "此项必填", trigger: "blur" },
          {
            validator: Valid.integerAndFloat,
            trigger: "blur"
          }
        ],
        source: [{ required: true, message: "支付方式必选", trigger: "blur" }],
        giveMoney: [
          {
            validator: Valid.integerAndFloat,
            trigger: "blur"
          }
        ]
      },
      typeNameArray: [] //种类标本
    };
  },
  mounted() {
    let th = this;
  },
  methods: {
    onSubmit() {
      let th = this;
      if (th.curItem.patientMoney < th.form.rechargeMoney) {
        th.$message.warning("退款金额超出账户余额，无法扣除，请正确输入！");
        return;
      }
      if (th.form.giveMoney) {
        if (th.form.giveMoney > th.curItem.grandPresenter) {
          th.$message.warning("赠送金额超出赠送余额，无法扣除，请正确输入！");
          return;
        }
      }
      let j = {};
      let add = function() {
        th.btnLoading = true;
        th.$api.rechargeReturn(j).then(res => {
          if (res.executed) {
            th.$message.success("操作成功");
            th.$emit("success");
            th.onClose();
          } else {
            th.$message.error(res.message);
          }
          th.btnLoading = false;
        });
      };
      th.$refs["form"].validate(v => {
        if (v) {
          th.form.hpId = th.adminUser.hpId;
          th.form.patientId = th.curItem.patientId;
          Object.assign(j, th.form);
          add();
        }
      });
    },
    onOpen() {
      let th = this;
      th.$nextTick(function() {
        if (th.curItem) {
          th.form.hpId = th.adminUser.hpId;
          th.form.levelId = th.curItem.levelId;
          th.form.patientId = th.curItem.patientId;
          th.form.levelTime = th.curItem.levelTime;
        }
      });
    },
    onClose() {
      let th = this;
      th.$refs["form"].resetFields();
      th.$emit("input", false);
    }
  }
};
</script>
